package com.inossem.warehousestorage_inossem.adapter.query;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.query.QueryOutStorageDetailActivity;
import com.inossem.warehousestorage_inossem.bean.query.QueryOutStorageDetailBean;
import com.inossem.warehousestorage_inossem.utils.QueryUtils;

import java.util.List;

/**
 * Created by leij on 2017/6/6.
 */

public class QueryOutStorageDetailAdapter extends BaseAdapter {

    private Context mContext;
    private List<QueryOutStorageDetailBean.DataBean.PositionListBean> mPositionList;
    private LayoutInflater inflater;
    private QueryOutStorageDetailActivity.QueryOutStorageDetailListener mQueryOutStorageDetailListener;
    private String mPosition;

    public QueryOutStorageDetailAdapter(Context context, List<QueryOutStorageDetailBean.DataBean.PositionListBean> positionList, String position, QueryOutStorageDetailActivity.QueryOutStorageDetailListener queryOutStorageDetailListener) {
        mContext = context;
        mPositionList = positionList;
        mQueryOutStorageDetailListener = queryOutStorageDetailListener;
        inflater = LayoutInflater.from(context);
        mPosition = position;
    }

    @Override
    public int getCount() {
        return mPositionList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return mPositionList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.view_in_storage_receive_list_first_item, null);
            EditText positionEditText = (EditText) convertView.findViewById(R.id.inStorageNumber);
            ImageView search = (ImageView) convertView.findViewById(R.id.search);
            positionEditText.setText(mPosition == null ? "" : mPosition);
            positionEditText.setHint(mContext.getResources().getString(R.string.activity_inventory_select_position_adapter_please_input_positionnumber));
//            inStorageNumber.addTextChangedListener(new QRCodeEditTextWatcher(inStorageNumber) {
//                @Override
//                public void onScan(String text) {
//                    Utils.closeSoftKeyboard(mContext, inStorageNumber);
//                    mInStorageReceiveListener.onNoDoubleClickListener(text);
//                }
//            });
            search.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    Utils.closeSoftKeyboard(mContext, v);
                    mQueryOutStorageDetailListener.onNoDoubleClickListener(positionEditText.getText().toString());
                }
            });
        } else {
            QueryOutStorageDetailBean.DataBean.PositionListBean positionItem = mPositionList.get(position - 1);
            ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.view_query_out_storage_detail, null);
                viewHolder = new ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, positionItem);
        }
        return convertView;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.serial = (TextView) view.findViewById(R.id.serial);
        viewHolder.position = (TextView) view.findViewById(R.id.position);
        viewHolder.materielCode = (TextView) view.findViewById(R.id.materielCode);
        viewHolder.batchNumber = (TextView) view.findViewById(R.id.batchNumber);
        viewHolder.materielDescription = (TextView) view.findViewById(R.id.materielDescription);
        viewHolder.areaNumber = (TextView) view.findViewById(R.id.areaNumber);
        viewHolder.number = (TextView) view.findViewById(R.id.number);
    }

    private void initData(Context context, ViewHolder viewHolder, View view, QueryOutStorageDetailBean.DataBean.PositionListBean positionItem) {
        viewHolder.serial.setText(positionItem.getSerial());
        viewHolder.position.setText(positionItem.getPosition());
        viewHolder.materielCode.setText(QueryUtils.changeFontColor(context, context.getResources().getString(R.string.view_query_out_storage_detail_materiel_code), positionItem.getMaterielCode(), R.color.common_font_item_name));
        viewHolder.batchNumber.setText(QueryUtils.changeFontColor(context, context.getResources().getString(R.string.view_query_out_storage_detail_batch_number), positionItem.getBatchNumber(), R.color.common_font_item_name));
        viewHolder.materielDescription.setText(positionItem.getMaterielDescription());
        viewHolder.areaNumber.setText(QueryUtils.changeFontColor(context, context.getResources().getString(R.string.view_query_out_storage_detail_area_number), positionItem.getAreaNumber(), R.color.common_font_item_name));
        viewHolder.number.setText(QueryUtils.changeFontColor(context, context.getResources().getString(R.string.view_query_out_storage_detail_number), positionItem.getNumber() + positionItem.getUnit(), R.color.common_font_item_name));
    }

    private class ViewHolder {
        private TextView serial, position, materielCode, batchNumber, materielDescription, areaNumber, number;
    }

}
